$(function () {
    var recharge_datagrid=$("#recharge_datagrid");
    var recharger=$("#recharger");
    var money=$("#money");
    var recharge_form=$("#recharge_form");

    var cmdObj={
        cardSnFormatter:function (value,row,index) {
            if(value){
                return value.cardSn;
            }
        },
        save:function () {
            //获取员工的id
            var memberId = $("#member_id").val();
            var operateType=$("input[name=operateType]:checked").val();
            var url=$("#save").data("url");
            console.log(operateType);
            var payType=$("input[name=payType]:checked").val();
            console.log(payType);
            if (!memberId) {
                $.messager.alert("提示","请加载一名会员信息!","info");
                return;
            }
            if (!operateType) {
                $.messager.alert("提示","请选择操作类型!","info");
                return;
            }
            if (!payType) {
                $.messager.alert("提示","请选择支付类型!","info");
                return;
            }

            //提交表单
            console.log(url);
            recharge_form.form("submit", {
                url: url+"?operateType="+operateType,
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("提示", "操作成功", "info", function () {
                            recharge_form.form("clear");
                            $("input[name='member.id']").val($("#cache_id").val());
                            recharge_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert("提示", data.msg, "error");
                    }
                }
            });
        }
    }
    //给data-cmd的元素绑定事件
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });

    $("#money input").blur(function () {
        var rechargeMoney=parseFloat($("#rechargeMoney").val());
        var giftMoney=parseFloat($("#giftMoney").val());
        var total=rechargeMoney+giftMoney;
        $("#totalRechargeMoney").text(total);
    });
    //搜索
    $("#searchMemberByKeyword").click(function () {
        var keyword=$("input[name=keyword]").val();
        //渲染下方数据表格
        recharge_datagrid.datagrid({
            url: '/recharge/list?keyword='+keyword,//数据来源,需要json,如果是资源,则请求这个资源以获取json
            fitColumns: true,
            pagination: true,//分页条
            singleSelect: true,//单选
            border: false,
            toolbar:"#recharge_dategrid_toolbar",
            columns: [[
                {field: 'member', title: '卡号', width: 100, formatter: cmdObj.cardSnFormatter},
                {field: 'operateType', title: '操作类型', width: 100},
                {field: 'payType', title: '支付类型', width: 100},
                {field: 'oldAccount', title: '变动前余额', width: 100},
                {field: 'rechargeMoney', title: '充值余额', width: 100},
                {field: 'giftMoney', title: '赠送余额', width: 100},
                {field: 'newAccount', title: '变动后余额', width: 100},
                {field: 'rechargeTime', title: '充值时间', width: 100},
                {field: 'remark', title: '备注', width: 100}
            ]]
        });
        //渲染左边信息栏
        $.post("/member/queryByKeyword",{keyword:keyword},function (data) {
            $("#m_carSn").text(data.cardSn);
            $("#m_username").text(data.username);
            $("#m_level").text(data.level.name);
            $("#m_birth").text(data.birthday);
            $("#m_totalAccount").text(data.totalAccount);
            $("#m_accumulation").text(data.accumulation);
            $("#m_consume").text(data.consume);
            $("input[name='member.id']").val(data.id);
            $("#cache_id").val(data.id);
            $("input[name=oldAccount]").val(data.totalAccount);
            $("#img").prop("src",data.imagePath);
        });
    });
    $("input[name='lateDays']").click(function () {
        var days = $(this).val();
        recharge_datagrid.datagrid("load",{
            keyword:$("input[name=keyword]").val(),
            lateDays:days
        })
    });
})